package com.ftg.learn.chapter13;

import java.math.BigDecimal;
import java.sql.SQLOutput;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * 学习一下stream
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/2/15 下午1:10
 */
public class LearnStream {

    public static void main(String[] args) {
        int[] as = new int[]{4, 4, 4, 1, 12, 4};
        System.out.println(Arrays.stream(as).min().getAsInt() + "通过optional输出 最小 ");
        System.out.println(Arrays.stream(as).max().getAsInt() + "通过optional输出 最大");
        System.out.println(Arrays.stream(as).distinct().sum() + "通过optional输出 总");

        //intstream 操作int
        IntStream intStream = Arrays.stream(as);
        long l = Arrays.stream(as).distinct().count();
        System.out.println(l);
        long l1 = Arrays.stream(as).distinct().filter(x -> x > 4).count();
        System.out.println(l1 + "长度");
        System.out.println(Arrays.toString(as));

        int[][] as1 = new int[][]{{4, 4, 4, 7, 12, 79}, {1, 2, 3, 561}};
        long l2 = Arrays.stream(as1).filter(x -> {
            for (int i : x) {
                if (i > 78) {
                    return true;
                }
            }
            return false;
        }).count();
        System.out.println(l2);

        Employeer[] emps = new Employeer[7];

        emps[0] = new Employeer("张三", new BigDecimal("10000"), "男", 1);
        emps[1] = new Employeer("李四", new BigDecimal("12000"), "男", 1);
        emps[2] = new Employeer("王五", new BigDecimal("18000"), "男", 1);
        emps[3] = new Employeer("李二狗", new BigDecimal("18000"), "男", 1);
        emps[4] = new Employeer("王春花", new BigDecimal("18000"), "男", 1);
        emps[5] = new Employeer("史小丽", new BigDecimal("18000"), "男", 1);
        emps[6] = new Employeer("高启强", new BigDecimal("18000"), "男", 1);

        Optional<Employeer> l4 = Arrays.stream(emps).min((x, y) -> x.getSal().subtract(y.getSal()).intValue());
//        Optional<Employeer> l4 = Arrays.stream(emps).max(Comparator.comparing(Employeer::getSal));
//        Optional<Employeer> l4 = Arrays.stream(emps).max((x, y)->x.getSal().compareTo(y.getSal()));


        //把所有员工的姓名输出出来
        Arrays.stream(emps).map(Employeer::getName).forEach(System.out::println);


//        Collectors 收集器帮助类，里面存在大量以to开头的方法，用来完成把当前stream收集成指定的集合操作
        List<String> empNames = Arrays.stream(emps).map(Employeer::getName).collect(Collectors.toList());
        System.out.println(empNames);

        double sals = Arrays.stream(emps).mapToDouble(x -> x.getSal().doubleValue()).sum();
        System.out.println(sals + "总工资");
        System.out.println(Math.round(sals/ emps.length) + "总工资");

//            BaseStream
//       intStream
//        Arrays.stream(emps).mapToDouble(x->x.getSal().doubleValue()).sum();
    }

}
